package com.autonavi.yunda.yunji.configuration;

import com.autonavi.yunda.yunji.core.engine.script.FunctionScriptClassManager;
import com.autonavi.yunda.yunji.core.engine.script.ScriptClassManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author cizhong.wcs
 * @date 2022/7/20 16:01
 */
@Slf4j
@Component
public class EngineInitConfiguration implements SmartInitializingSingleton {

    @Value("${yunji.init.interface.lazy:false}")
    private Boolean lazyInit;
    @Resource
    private ScriptClassManager scriptClassManager;
    @Resource
    private FunctionScriptClassManager functionScriptClassManager;

    @Override
    public void afterSingletonsInstantiated() {
        log.info("checkAndRefreshFunctionScriptManager start");
        functionScriptClassManager.checkAndRefreshFunctionScriptManager();
        log.info("checkAndRefreshFunctionScriptManager end");
        if (!lazyInit) {
            log.info("initScriptClassManager start");
            scriptClassManager.checkAndRefreshScriptClassManager();
            log.info("initScriptClassManager end");
        } else {
            log.warn("lazy init scriptClassManager,call http://localhost:8001/__refreshScriptClassCache?id=xxx to register  ");
        }
    }
}
